layui.use(['form', 'layer', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //角色列表
    var tableIns = table.render({
        elem: '#managerList',
        url: '/Manager/LoadData/',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        id: "managerListTable",
        cols: [[
            { type: "checkbox", fixed: "left", width: 50 },
            { field: "Id", title: 'Id', width: 50, align: "center" },
            { field: 'UserName', title: '登陆ID', minWidth: 50, align: "center" },
            { field: 'RealName', title: '用户名', minWidth: 50, align: "center" },
            { field: 'Mobile', title: '手机号码', minWidth: 80, align: "center" },
            { field: 'Email', title: '邮箱地址', minWidth: 100, align: "center" },
            { field: 'RoleName', title: '所属角色', minWidth: 80, align: 'center' },
            { field: 'State', title: '状态', minWidth: 100, fixed: "right", align: "center", templet: '#State' },
            { title: '操作', minWidth: 80, templet: '#managerListBar', fixed: "right", align: "center" }
        ]]
    });

    var userInfo = JSON.parse(window.sessionStorage.getItem('userInfo'));
    if (userInfo) {
        // 只有一级代理可以注册代理，中级代理只能注册直客
        //if (userInfo.roleId == 3 || userInfo.roleId == 4 || userInfo.roleId == 5) {
        if (userInfo.roleId == 3) {
            $(".code_btn").show();
        } else if (userInfo.roleId == 1 || userInfo.roleId == 2) {
            $(".addManager_btn, .delAll_btn").show();
        }
    }

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() !== '' || $(".searchState").val() !== '' || $(".searchRoleId").val() !== '') {
            table.reload("managerListTable", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val(),  //搜索的关键字
                    state: $(".searchState").val(),
                    roleId: $(".searchRoleId").val()
                }
            });
        } else {
            layer.msg("请输入搜索的内容");
        }
    });


    function previewManager(edit) {
        var tit = "查看用户";
        var index = layui.layer.open({
            title: tit,
            type: 2,
            anim: 1,
            area: ['500px', '90%'],
            content: "/Manager/Preview/",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find("#Id").val(edit.Id);
                    body.find(".UserName").val(edit.UserName);
                    body.find(".NickName").val(edit.NickName);
                    body.find(".RoleId").val(edit.RoleId);
                    body.find(".Mobile").val(edit.Mobile);
                    body.find(".Email").val(edit.Email);
                    body.find(".BankType").val(edit.BankType);
                    body.find(".BankCard").val(edit.BankCard);

                    var html = '';
                    for (var i = 0; i < edit.AuditImages.length; i++) {
                        var source = edit.AuditImages[i];
                        var src = 'http://dns.qsgjlive.com/' + source;
                        //html += '<li><img id="img'+ i +'" layer-src="' + src + '" src="' + src + '" alt="' + source + '" class="layui-upload-img" style="width: 60px; height: 60px;"></li>'
                        html += '<a target="_blank" href="' + src + '"><img src="' + src + '" style="width: 60px; height: 60px;margin: 2px;"></a>';
                    }
                    body.find(".auditImages").html(html);
                    
                    body.find("input:checkbox[name='IsLock']").prop("checked", edit.IsLock);
                    body.find(".Remark").text(edit.Remark || '');
                    form.render();
                }
            }
        });
    }

    
    $(".addManager_btn").click(function () {
        addManager();
    });

    //批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('managerListTable'),
            data = checkStatus.data,
            managerId = [];
        if (data.length > 0) {
            for (var i in data) {
                managerId.push(data[i].Id);
            }
            layer.confirm('确定删除选中的用户？', { icon: 3, title: '提示信息' }, function (index) {
                //获取防伪标记
                del(managerId);
            });
        } else {
            layer.msg("请选择需要删除的用户");
        }
    });



    //列表操作
    table.on('tool(managerList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;

        if (layEvent === 'preview') { //
            previewManager(data); 
        } else if (layEvent === 'exchangeRate') { //出入金汇率
            settingExchangeRate(data.Id);
        }
    });

    form.on('switch(IsLock)', function (data) {
        var tipText = '确定锁定当前用户吗？';
        if (!data.elem.checked) {
            tipText = '确定启用当前用户吗？';
        }
        layer.confirm(tipText, {
            icon: 3,
            title: '系统提示',
            cancel: function (index) {
                data.elem.checked = !data.elem.checked;
                form.render();
                layer.close(index);
            }
        }, function (index) {
            changeLockStatus(data.value, data.elem.checked);
            layer.close(index);
        }, function (index) {
            data.elem.checked = !data.elem.checked;
            form.render();
            layer.close(index);
        });
    });
    




    function copyToClipboard(txt) {
        var window = top.window;

        if (window.clipboardData) {
            window.clipboardData.clearData();
            clipboardData.setData("Text", txt);
            window.alert("复制成功！");
        } else if (navigator.userAgent.indexOf("Opera") != -1) {
            window.location = txt;
        } else if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            } catch (e) {
                window.alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'");
            }
            var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
            if (!clip)
                return;
            var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
            if (!trans)
                return;
            trans.addDataFlavor("text/unicode");
            var str = new Object();
            var len = new Object();
            var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
            var copytext = txt;
            str.data = copytext;
            trans.setTransferData("text/unicode", str, copytext.length * 2);
            var clipid = Components.interfaces.nsIClipboard;
            if (!clip)
                return false;
            clip.setData(trans, null, clipid.kGlobalClipboard);
            window.alert("复制成功！");
        }
    }
    

    // 出入金汇率设置
    function settingExchangeRate(managerId) {
        //debugger;
        var index = layui.layer.open({
            title: '出入金汇率设置',
            type: 2,
            anim: 1,
            area: ['800px', '50%'],
            content: "/Manager/SettingExchangeRate?managerId=" + managerId,
            success: function (layero, index) {

               

            }
        });
    }

});
